<?php
/**
 * 用户相关接口
 * 接受数据格式:POST
 * 返回数据格式:JSON
 * 
 * @author Wayne(wayneyu@flaringtech.com)
 * 
 */
defined('BASEPATH') OR exit('No direct script access allowed');
class Message extends Jiuchi_Controller {
	private $returnUrl;
	private $model;

    function __construct() {
        parent::__construct();
        $this->load->model('user_model');
		$this->load->model('user_dynamic_model');
		$this->load->model('circle_model');
		$this->load->model('topic_model');
		$this->load->model('comment_model');
		$this->load->model('message_inform_model');
		$this->load->model('message_at_model');
		$this->load->model('message_letter_model');
    }
    
    /**
     * 用户获取通知列表接口
     *
     * 页面	:登陆页面
     * URL	:/message/getinformlist
     * 备注	:
     * 验证	:是
     * 接受参数:PageSize, CurPage
     * 返回参数:[{Id,Status,Title,Msg,CreatedAt},{},...]
     */
    public function getinformlist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 2 || !isset($input['PageSize']) || !isset($input['CurPage'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}
    	
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	
    	$hintArr = array(
    			1	=>	'评论了你的话题',
    			2	=>	'回复了你的评论',
    			3	=>	'回复了你',
    			4	=>	'赞了你的评论'
    	);
    	$urlArr = array(
    			1	=>	'topic',
    			2	=>	'comment',
    			3	=>	'comment',
    			4	=>	'comment'
    	);
    	
    	$data = $this->message_inform_model->getUserInforms($this->mytoken->userdata('uid'), $input['PageSize'], $input['PageSize']*$input['CurPage']);
    	foreach ($data as $key => $val){
    		$uInfo  = $this->user_model->getInfo($val['informer_id']);
    		switch ($val['inform_type']){
    			case 1:
    				$objInfo	= $this->topic_model->getInfo($val['obj_id']);
    				$circleName = $objInfo['circle_name'];
    				$content = $objInfo['content'];
    				$commentsNumber = $objInfo['comments_number'];
    				break;
    			case 2:
    			case 3:
    			case 4:
    				$objInfo	= $this->comment_model->getInfo($val['obj_id']);
    				$tInfo = $this->topic_model->getInfo($objInfo['topic_id']);
    				$circleName = $tInfo['circle_name'];
    				$content = '';
    				$commentsNumber = '';
    				break;
    			default:
    				break;
    		}
    		$results[$key] = array(
    				'Name'		=>	isset($uInfo['nickname'])?$uInfo['nickname']:'',
    				'Hint'		=>	isset($hintArr[$val['inform_type']])?$hintArr[$val['inform_type']]:'',
    				'Title'		=>	isset($val['title'])?$val['title']:'',
    				'Avatar'	=>	site_url($uInfo['headportrait']),
    				'ObjId'		=>	$val['obj_id'],
    				'ObjType'	=>	isset($urlArr[$val['inform_type']])?$urlArr[$val['inform_type']]:'',
    				'CircleName'	=>	$circleName,
    				'CommentsNumber'=>	$commentsNumber,
    				'Content'		=>	$content,
    				'Datetime'		=>	$this->user_dynamic_model->getTimeFormat(strtotime($val['created_at'])),
    		);
    	}
    	
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
    
    /**
     * 用户获取@列表接口
     *
     * 页面	:登陆页面
     * URL	:/message/getatlist
     * 备注	:
     * 验证	:是
     * 接受参数:PageSize, CurPage
     * 返回参数:[{Id,Status,Title,Msg,CreatedAt},{},...]
     */
    public function getatlist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 2 || !isset($input['PageSize']) || !isset($input['CurPage'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}
    	 
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	 
		$urlArr = array(
				1	=>	'topic/view/',
				2	=>	'comment/view/'
		);
    	 
    	$data = $this->message_at_model->getUserAts($this->mytoken->userdata('uid'), $input['PageSize'], $input['PageSize']*$input['CurPage']);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['ater_id']);
			$results[$key] = array(
					'Avatar'	=>	site_url($uInfo['headportrait']),
					'Name'		=>	isset($uInfo['nickname'])?$uInfo['nickname']:'',
					'link'		=>	'<a href="'.site_url($urlArr[$val['at_type']].$val['obj_id']).'">@了你</a>',
					'Time'		=>	$this->user_dynamic_model->getTimeFormat(strtotime($val['created_at']))
			);
		}
    	 
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
    
    /**
     * 用户获取私信列表接口
     *
     * 页面	:登陆页面
     * URL	:/message/getprivatelist
     * 备注	:
     * 验证	:是
     * 接受参数:PageSize, CurPage
     * 返回参数:[{Id,Status,Title,Msg,CreatedAt},{},...]
     */
    public function getprivatelist(){
    	//初始化
    	$input = $this->input->post();
    	$results = array();
    	//检测参数及授权合法性
    	$this->checkToken();
    	if(count($input) != 2 || !isset($input['PageSize']) || !isset($input['CurPage'])){
    		$this->apioutput->apiResponseError('接口参数不正确！');
    	}
    	
    	!empty($input['CurPage'])
    	&& $input['CurPage'] = $this->varfilter->checkInt($input['CurPage']);
    	if(empty($input['CurPage'])){
    		$input['CurPage'] = 1;
    	}
    	$input['CurPage'] -= 1;
    	!empty($input['PageSize'])
    	&& $input['PageSize'] = $this->varfilter->checkInt($input['PageSize']);
    	if(empty($input['PageSize'])){
    		$this->apioutput->apiResponseError('参数异常');
    	}
    	
    	$data = $this->message_letter_model->getUserLetters($this->mytoken->userdata('uid'), $input['PageSize'], $input['PageSize']*$input['CurPage']);
		foreach ($data as $key => $val){
			$uInfo  = $this->user_model->getInfo($val['sender_id']);
			$results[$key] = array(
					'Avatar'	=>	site_url($uInfo['headportrait']),
					'Name'		=>	isset($uInfo['nickname'])?$uInfo['nickname']:'',
					'Content'	=>	$val['content'],
					'link'		=>	'<a href="'.site_url('mycenter/letter/'.$val['id']).'">私信了你</a>'
			);
		}
    	
    	//返回结果
    	$this->apioutput->apiResponseSuccess($results);
    }
}